package goTeam.up.cs301.go;

import goTeam.up.cs301.game.Game;
import goTeam.up.cs301.game.GamePlayer;
import goTeam.up.cs301.game.infoMsg.GameInfo;
import goTeam.up.cs301.game.infoMsg.NotYourTurnInfo;

/**
 * This is a really dumb computer player that always just makes a random move
 * it's so stupid that it sometimes tries to make moves on non-blank spots.
 * 
 * @author Steven R. Vegdahl
 * @author Matthew Young
 * @version 16 November 2013 
 */
public class GoComputerPlayer1 extends GoComputerPlayer
{
    /*
     * Constructor for the GoComputerPlayer1 class
     */
    public GoComputerPlayer1(String name) {
        // invoke superclass constructor
        super(name); // invoke superclass constructor
    }

    /**
     * Called when the player receives a game-state (or other info) from the
     * game.
     * 
     * @param gameInfo
     * 		the message from the game
     */
    @Override
    protected void receiveInfo(GameInfo info) {
    	
    	// if it was a "not your turn" message, just ignore it
    	if (info instanceof NotYourTurnInfo) return;
    	
    	makeRandomMove(info, game, this);
    	
    }

	/**
	 * Randomly make a move, following what the human player did 98% of the time with
	 * regard to passing.
	 * @param info	So we know whether or not the human player passed
	 */
	protected static void makeRandomMove(GameInfo info, Game game, GamePlayer p) {
		// pick x and y positions at random (0-NUM_LINES)
    	int xVal = (int)((GoHumanPlayer1.NUM_LINES)*(Math.random()-Double.MIN_NORMAL));
    	int yVal = (int)((GoHumanPlayer1.NUM_LINES)*(Math.random()-Double.MIN_NORMAL));
    	
    	

    	// We can't put artificial delay because the Go board is so much larger than the TTT board
    	
    	// Submit our move to the game object. We haven't even checked it it's
    	// our turn, or that that position is unoccupied. If it was not our turn,
    	// we'll get a message back that we'll ignore. If it was an illegal move,
    	// we'll end up here again (and possibly again, and again). At some point,
    	// we'll end up randomly picking a move that is legal.
    	
    	// We just check to see if the last move was a pass and act likewise 98% of the time
    	game.sendAction(new GoMoveAction(p, yVal, xVal, info instanceof GoState ? ((GoState)info).wasLastMoveAPass() ^ (Math.random() < .02) : Math.random() < .02));
	}
}
